/**
 * 用户密码登陆的方法
 * @return {boolean}
 * */
function PhoneLogin() {
    //用户是否点击登录的标识变量
    var username = $('#username').val();
    var password = $('#password').val();
    if (username === null || username === "") {
        if (password === null || password === "") {
            $('#error-phone-email').show();
            $('#error-password').show();
            return false;
        } else {
            $('#error-phone-email').show();
            return false;
        }
    } else {
        if (password === null || password === "") {
            $('#error-password').show();
            return false;
        }
    }
    /*$.ajax({
        url: '/login/userLogin',
        type: 'POST',
        data: {"keHuUsePhone": username, "keHuUsePassWord": password},
        dataType: 'json',
        success: function (data) {
            layui.use(['layer'], function () {
                console.log(data)
                if (data.code === 0) {
                    layer.msg(data.msg, {icon: 6});
                    window.location.href = '../../../weXin/PC/index.html';
                }
                if (data.code === 500) {
                    layer.msg("登陆失败：" + data.msg, {icon: 5});
                }
            })
        }
    })*/

    /*$.ajax({
        url: '/login/userLogin',
        type: 'POST',
        data: {"keHuUsePhone": username, "keHuUsePassWord": password},
        dataType: 'json',
        success:function (data) {
            console.log(data);
            layui.use(['layer'], function () {
                if(data.code==0){
                    layer.msg("登录成功",{icon:6});
                    setTimeout(function () {
                        window.location.href = '../../weXin/PC/index.html';
                    },200);
                }else{
                    layer.msg(data.msg,{icon:5});
                }
            })
        }
    })*/
    $.ajax({
        // url:'http://192.168.101.9:8032/user/userLog',
        // url:'http://111.229.111.45:8032/user/userLog',
        url: '/login/keHuUserLogin',
        type: 'POST',
        data: {"phone": username, "passwords": password},
        dataType: 'json',
        success: function (data) {
            console.log(data)
            layui.use(['layer'], function () {
                let layer = layui.layer;
                if (data.code == 200) {
                    let accessToken = data.data.accessToken;
                    layer.msg("登录成功", {icon: 6});
                    setTimeout(function () {
                        console.log("总");
                        window.location.href = '/weXin/PC/caiWuAdmin.html?accessToken=' + accessToken;
                        //window.location.reload();
                    }, 500);


                } else if (data.code == 500) {
                    layer.msg("账号或密码有误", {icon: 5});
                }
            });
        }
    });

}

//验证码登录
function PhoneYZMLogin() {
    //用户是否点击登录的标识变量
    var phone = $('#userphone').val();
    //获取验证码，YZM：验证码
    var YZM = $('#verification').val();
    if (phone === null || phone === "") {
        if (YZM === null || YZM === "") {
            $('#error-phone-email').show();
            $('#error-phone-verification').show();
            return false;
        } else {
            $('#error-phone-email').show();
            return false;
        }
    } else {
        if (YZM === null || YZM === "") {
            $('#error-phone-verification').show();
            return false;
        }
    }
    $.ajax({
        url: '/login/phoneLogin',
        type: 'POST',
        data: {"keHuUsePhone": phone, "YZM": YZM},
        dataType: 'json',
        success: function (data) {
            layui.use(['layer'], function () {
                console.log(data)
                if (data.code === 0) {
                    layer.msg(data.msg, {icon: 6});
                    window.location.href = '../../../index.html';
                }
                if (data.code === 500) {
                    layer.msg("登陆失败：" + data.msg, {icon: 5});
                }
            });
        }
    });
}

/*
* 获取手机验证码的方法
* */
function loginPhoneCode() {
    layui.use("layer", function () {
        let layer = layui.layer;
        layer.msg("已点击，请勿连续点击");
        var phone = $('#userphone').val();
        var phoneReg = /^1[3456789]\d{9}$/;
        if (phone == null || phone === "") {
            $('#error-phone').show();
            return false;
        } else if (!phoneReg.test(phone)) {
            layer.msg("手机号必须为11位数字");
            return false;
        }
        console.log("phone")
        console.log(phone)
        $.post("/login/verification", {"keHuUsePhone": phone}, function (data) {
            if (data.code === 0) {
                $('#phone-code-login').val("     已发送");
                $('#phone-code-login').attr('disabled', true);
                $('#phone-code-login').css('color', 'gray');
            } else if (data.code === 500) {
                layer.msg(data.msg);
            }
        });
    });
}

if (window.parent != window) {
    window.parent.location.reload(true);
}